package com.brayan.melinxa.model;

import java.io.IOException;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ApplicationSettings {
	private static Properties props = new Properties();
	//prop keys
	private final static String LOCALIZATION = "localization";
	private final static String INVOKE_ON_OS_STARTUP = "invokeOnOsStartup";
	private final static String ACTIVATE_ON_STARTUP = "activateOnStartup";
	
	public ApplicationSettings() throws SecurityException, IOException {
		props = ApplicationSettingsPersistence.getApplicationPropertyFile();
	}

	public static String getLocalization() {
		return props.getProperty(LOCALIZATION);
	}

	/**
	 * The in18 localization string. That is en or ch-De, or fr-FR, etc.
	 * 
	 * @param localization
	 *            Pass a valid in18 string.
	 */
	public static void setLocalization(String localization) throws IllegalArgumentException {
		Pattern pattern;
		Matcher matcher;

		final String IN18_PATTERN = "[a-z]{2}-[A-Z]{2}";
		pattern = Pattern.compile(IN18_PATTERN);
		matcher = pattern.matcher(localization);
		if (matcher.matches()){
			props.setProperty(LOCALIZATION, localization);
		}
		else
			throw new IllegalArgumentException("The passed in18 localization string ("
					+ localization + ") fails to match it's pattern.");		
	}

	public static void setInvokeOnOsStartup(boolean invokeOnOsStartup) {
		props.setProperty(INVOKE_ON_OS_STARTUP, String.valueOf(invokeOnOsStartup));
	}

	public static boolean isInvokeOnStartup() {
		return Boolean.parseBoolean(props.getProperty(INVOKE_ON_OS_STARTUP));
	}

	public static void setActivateOnStartup(boolean activateOnOsStartup) {		
		props.setProperty(ACTIVATE_ON_STARTUP, String.valueOf(activateOnOsStartup));
	}

	public static boolean isActivateOnOsStartup() {
		return Boolean.parseBoolean(props.getProperty(ACTIVATE_ON_STARTUP));
	}

}
